package com.zxlspace.collection;


public class SxtArrayList {

    private int size;

    private Object[] elementData;

    public SxtArrayList() {
        this(10);
    }

    public SxtArrayList(int initialCapacity) {
        if (initialCapacity > 0) {
            this.elementData = new Object[initialCapacity];
        }else if (initialCapacity == 0) {
            this.elementData = new Object[10];
        }else {
            throw new IllegalArgumentException();
        }
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return 0==size;
    }

    public boolean add(Object obj) {
        //数组扩容和数据拷贝
        if (size == elementData.length) {
            Object[] newArray = new Object[size*2 + 1];
            System.arraycopy(elementData, 0, newArray, 0, elementData.length);
            elementData = newArray;
        }
        this.elementData[size++] = obj;
        return true;
    }

    //检查索引是否越位
    private void rangeCheck(int index) {
        if (index<0 || index >= size) {
            throw new IndexOutOfBoundsException();
        }
    }

    public Object get(int index) {
        rangeCheck(index);
        return elementData[index];
    }

    public boolean remove(int index) {
        rangeCheck(index);
        int numMoved = size - index - 1; //数组元素向前移动位数
        if (numMoved > 0) {
            System.arraycopy(elementData, index+1, elementData, index, numMoved);
        }

        elementData[--size] = null; //数组向前移动后，最后一位置空
        return true;
    }

    public void remove(Object obj) {
        for (int i=0;i<size; i++) {
            if (get(i).equals(obj)) {  //注意底层调用的是equals而不是==
                remove(i);
            }
        }
    }

    public Object set(int index, Object obj) {
        rangeCheck(index);
        Object oldValue = elementData[index];
        elementData[index] = obj;
        return oldValue;
    }

    public void add(int index, Object obj) {
        rangeCheck(index);
        ensualCapacity();
        System.arraycopy(elementData, index, elementData, index + 1,
                size - index);
        elementData[index] = obj;
        size++;
    }

    //数组扩容和拷贝
    private void ensualCapacity() {
        if (size == elementData.length) {
            Object[] newArray = new Object[size*2 + 1];
            System.arraycopy(elementData, 0, newArray, 0, elementData.length);
            elementData = newArray;
        }
    }

    public static void main(String[] args) {
        SxtArrayList list = new SxtArrayList(5);
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("111");
        list.add("222");
        list.add("333");

        Object object = list.get(4);
        System.out.println(object);
        System.out.println(list);
//        list.remove(4);
//        list.remove("333");
        list.set(2, "fff");
        list.add(3,"哈哈哈");

        System.out.println(list.size());

        /*System.out.println("######################");

        String aaa = "aaa";
        String bbb = "aaa";
        System.out.println(aaa == bbb);
        System.out.println(aaa.equals(bbb));

        System.out.println(new Dog().equals(new Dog()));   //比较内存地址的引用
        Dog dog1 = new Dog();
        Dog dog2 = new Dog();
        System.out.println(dog1);
        System.out.println(dog2);
        System.out.println(dog1 == dog2);*/

        for (int i =0; i<list.size(); i++ ) {
            System.out.println(list.get(i));
        }

        Dog dog = new Dog();
        String s = new String();
        s.length();
    }
}

class Dog{

}
